using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class office_acl : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int ret_code;

        try
        {
            SqlCommand cmd = new SqlCommand("sp_add_office_acl");
            SqlParameter para = cmd.Parameters.AddWithValue("@office_type",lstOfficeTypes.SelectedValue);
            para = cmd.Parameters.AddWithValue("@func_id",lstAvailFunc.SelectedValue);

            para = cmd.Parameters.Add("ret_code", SqlDbType.Int);
            para.Direction = ParameterDirection.ReturnValue;
            TinyDAL.Exec(ref cmd);
            ret_code = (int)para.Value;
            if (ret_code == 0)
                lblError.Text = "Done.";
            else if (ret_code == 1)
                lblError.Text = "Adding failed!";
            else
                lblError.Text = "Unknown error!";
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
        GridView1.DataBind();
        lstAvailFunc.DataBind();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        
        int ret_code;
        string key;
        string[] strArray;
        if (Request.Form["KEY"] == null)
        {
            lblError.Text = "Please select the function you want to delete.";
            return;
        }
        key = Request.Form["KEY"];
        strArray = key.Split('|');

        SqlCommand cmd = new SqlCommand();
        lblError.Text = "";
        try
        {
            cmd.CommandText = "sp_delete_office_acl";
            SqlParameter para = cmd.Parameters.AddWithValue("@office_type", strArray[0]);
            para = cmd.Parameters.AddWithValue("@func_id", strArray[1]);
            para = cmd.Parameters.Add("ret_code", SqlDbType.Int);
            para.Direction = ParameterDirection.ReturnValue;
            TinyDAL.Exec(ref cmd);
            ret_code = (int)para.Value;
            if (ret_code == 0)
                lblError.Text = "Done.";
            else
                lblError.Text = "delete failed!";
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }

        GridView1.DataBind(); 
        lstAvailFunc.DataBind();
    }
}
